<?php
declare (strict_types = 1);

namespace app\install\controller;

use app\BaseController;
use org\File;
use think\facade\Env;
use think\facade\View;
use think\exception\ValidateException;
use app\install\validate\DbCheck as DbValidate;

class Index extends BaseController
{

    protected function initialize(){
        $this->lock = root_path().'app/install/data/install.lock';
        if(File::has($this->lock)){
            return return_jump_tip(0, '已经成功安装了，请不要重复安装!', '/');
        }
    }

    public function index(){
        return view('install/index');
    }

    /**
     * 安装环境检测
     */
    public function detect() {
        $list = check_dirfile();
        $funList = check_func();
        View::assign('list', $list);
        View::assign('funList', $funList);
        return view('install/detect');
    }

    /**
     * 配置系统
     */
    public function config() {
        return view('install/config');
    }

    public function install() {
        set_time_limit(0);
        header('Cache-Control: no-cache');
        header('X-Accel-Buffering: no');
        echo View::fetch('install/install');
        ob_end_flush();
        ob_implicit_flush(1);
        //检测信息
        $data = request()->post();
        check_data($data);
        show_msg('安装信息验证成功!');
        $db=check_db($data);
        show_msg('数据库检查完成...');
        write_config($data);
        create_tables($db,$data['prefix']);
        show_msg('创建基础数据库完成...');
        register_administrator($db,$data);
        File::put($this->lock,time());
        complete('安装程序执行完毕！');
        install_stop();
    }

}
